{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "1. 上涨\n",
    "\n",
    "2. 下跌\n",
    "\n",
    "3. 震荡\n",
    "\n",
    "震荡上涨\n",
    "震荡下跌\n",
    "\n",
    "根据传入的数据、时间段，判断这个时间段是什么趋势；\n",
    "而非自己拆分这段时间的趋势；\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import sys\n",
    "import numpy as np\n",
    "\n",
    "sys.path.append('..')\n",
    "from common import candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>times</th>\n",
       "      <th>highp</th>\n",
       "      <th>openp</th>\n",
       "      <th>lowp</th>\n",
       "      <th>nowv</th>\n",
       "      <th>preclose</th>\n",
       "      <th>curvol</th>\n",
       "      <th>curvalue</th>\n",
       "      <th>signType</th>\n",
       "      <th>dkWarnType</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-10-21</td>\n",
       "      <td>3610.96</td>\n",
       "      <td>3590.05</td>\n",
       "      <td>3576.35</td>\n",
       "      <td>3594.78</td>\n",
       "      <td>3587.00</td>\n",
       "      <td>3.523109e+10</td>\n",
       "      <td>4.535717e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-10-20</td>\n",
       "      <td>3596.05</td>\n",
       "      <td>3583.24</td>\n",
       "      <td>3574.30</td>\n",
       "      <td>3587.00</td>\n",
       "      <td>3593.15</td>\n",
       "      <td>3.442450e+10</td>\n",
       "      <td>4.504830e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-10-19</td>\n",
       "      <td>3596.79</td>\n",
       "      <td>3562.30</td>\n",
       "      <td>3560.62</td>\n",
       "      <td>3593.15</td>\n",
       "      <td>3568.14</td>\n",
       "      <td>3.341977e+10</td>\n",
       "      <td>4.401815e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-10-18</td>\n",
       "      <td>3571.05</td>\n",
       "      <td>3571.05</td>\n",
       "      <td>3539.48</td>\n",
       "      <td>3568.14</td>\n",
       "      <td>3572.37</td>\n",
       "      <td>3.409506e+10</td>\n",
       "      <td>4.557879e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-10-15</td>\n",
       "      <td>3578.77</td>\n",
       "      <td>3551.99</td>\n",
       "      <td>3542.69</td>\n",
       "      <td>3572.37</td>\n",
       "      <td>3558.28</td>\n",
       "      <td>3.202930e+10</td>\n",
       "      <td>4.259247e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        times    highp    openp     lowp     nowv  preclose        curvol  \\\n",
       "0  2021-10-21  3610.96  3590.05  3576.35  3594.78   3587.00  3.523109e+10   \n",
       "1  2021-10-20  3596.05  3583.24  3574.30  3587.00   3593.15  3.442450e+10   \n",
       "2  2021-10-19  3596.79  3562.30  3560.62  3593.15   3568.14  3.341977e+10   \n",
       "3  2021-10-18  3571.05  3571.05  3539.48  3568.14   3572.37  3.409506e+10   \n",
       "4  2021-10-15  3578.77  3551.99  3542.69  3572.37   3558.28  3.202930e+10   \n",
       "\n",
       "       curvalue  signType  dkWarnType  \n",
       "0  4.535717e+11         0           0  \n",
       "1  4.504830e+11         0           0  \n",
       "2  4.401815e+11         0           0  \n",
       "3  4.557879e+11         0           0  \n",
       "4  4.259247e+11         0           0  "
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = 'F:\\HQData\\market_A\\\\'\n",
    "weekpath = 'F:\\HQData\\week_A\\\\'\n",
    "file_index = path + \"index_SZZS.csv\"\n",
    "file_jake = weekpath +\"002459.csv\"\n",
    "\n",
    "df = pd.read_csv(file_index)\n",
    "# df=pd.read_csv(file_jake)\n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>High</th>\n",
       "      <th>Open</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>preclose</th>\n",
       "      <th>Volume</th>\n",
       "      <th>curvalue</th>\n",
       "      <th>signType</th>\n",
       "      <th>dkWarnType</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>times</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>3369.28</td>\n",
       "      <td>3258.63</td>\n",
       "      <td>3253.88</td>\n",
       "      <td>3350.52</td>\n",
       "      <td>3234.68</td>\n",
       "      <td>5.313523e+10</td>\n",
       "      <td>5.497601e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>3394.22</td>\n",
       "      <td>3330.80</td>\n",
       "      <td>3303.18</td>\n",
       "      <td>3351.45</td>\n",
       "      <td>3350.52</td>\n",
       "      <td>5.016614e+10</td>\n",
       "      <td>5.323985e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>3374.90</td>\n",
       "      <td>3326.65</td>\n",
       "      <td>3312.21</td>\n",
       "      <td>3373.95</td>\n",
       "      <td>3351.45</td>\n",
       "      <td>3.919188e+10</td>\n",
       "      <td>4.364167e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>3381.57</td>\n",
       "      <td>3371.96</td>\n",
       "      <td>3285.10</td>\n",
       "      <td>3293.46</td>\n",
       "      <td>3373.95</td>\n",
       "      <td>3.711311e+10</td>\n",
       "      <td>3.992303e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>3404.83</td>\n",
       "      <td>3276.97</td>\n",
       "      <td>3267.51</td>\n",
       "      <td>3285.41</td>\n",
       "      <td>3293.46</td>\n",
       "      <td>4.102408e+10</td>\n",
       "      <td>4.586480e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               High     Open      Low    Close  preclose        Volume  \\\n",
       "times                                                                    \n",
       "2015-01-05  3369.28  3258.63  3253.88  3350.52   3234.68  5.313523e+10   \n",
       "2015-01-06  3394.22  3330.80  3303.18  3351.45   3350.52  5.016614e+10   \n",
       "2015-01-07  3374.90  3326.65  3312.21  3373.95   3351.45  3.919188e+10   \n",
       "2015-01-08  3381.57  3371.96  3285.10  3293.46   3373.95  3.711311e+10   \n",
       "2015-01-09  3404.83  3276.97  3267.51  3285.41   3293.46  4.102408e+10   \n",
       "\n",
       "                curvalue  signType  dkWarnType  \n",
       "times                                           \n",
       "2015-01-05  5.497601e+11         0           0  \n",
       "2015-01-06  5.323985e+11         0           0  \n",
       "2015-01-07  4.364167e+11         0           0  \n",
       "2015-01-08  3.992303e+11         0           0  \n",
       "2015-01-09  4.586480e+11         0           0  "
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = df[df.times>'2015']\n",
    "df1 = df1.sort_values('times')\n",
    "df1.set_index('times', inplace=True)\n",
    "df1.rename( columns={'times':'Date','openp':'Open', 'highp':'High','lowp':'Low',\"nowv\":'Close','curvol':\"Volume\"}, inplace=True)\n",
    "\n",
    "df1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "上涨趋势从【2015-03-06】开始， 到【2015-03-25】结束; 持续【13】天；\n",
      "上涨趋势从【2015-04-02】开始， 到【2015-04-21】结束; 持续【12】天；\n",
      "上涨趋势从【2015-05-19】开始， 到【2015-05-29】结束; 持续【8】天；\n",
      "上涨趋势从【2015-06-01】开始， 到【2015-06-11】结束; 持续【8】天；\n",
      "上涨趋势从【2015-07-17】开始， 到【2015-07-27】结束; 持续【6】天；\n",
      "上涨趋势从【2015-10-14】开始， 到【2015-10-22】结束; 持续【6】天；\n",
      "上涨趋势从【2015-12-14】开始， 到【2015-12-24】结束; 持续【8】天；\n",
      "上涨趋势从【2016-06-28】开始， 到【2016-07-08】结束; 持续【8】天；\n",
      "上涨趋势从【2016-08-03】开始， 到【2016-08-17】结束; 持续【10】天；\n",
      "上涨趋势从【2017-01-03】开始， 到【2017-01-11】结束; 持续【6】天；\n",
      "上涨趋势从【2017-07-19】开始， 到【2017-07-27】结束; 持续【6】天；\n",
      "上涨趋势从【2017-10-20】开始， 到【2017-10-30】结束; 持续【6】天；\n",
      "上涨趋势从【2017-12-29】开始， 到【2018-01-11】结束; 持续【8】天；\n",
      "上涨趋势从【2018-01-17】开始， 到【2018-01-30】结束; 持续【9】天；\n",
      "上涨趋势从【2019-01-11】开始， 到【2019-01-22】结束; 持续【7】天；\n",
      "上涨趋势从【2019-01-31】开始， 到【2019-02-15】结束; 持续【6】天；\n",
      "上涨趋势从【2019-03-28】开始， 到【2019-04-09】结束; 持续【7】天；\n",
      "上涨趋势从【2019-12-25】开始， 到【2020-01-03】结束; 持续【6】天；\n",
      "上涨趋势从【2020-04-29】开始， 到【2020-05-12】结束; 持续【6】天；\n",
      "上涨趋势从【2020-06-30】开始， 到【2020-07-10】结束; 持续【8】天；\n",
      "上涨趋势从【2020-07-28】开始， 到【2020-08-05】结束; 持续【6】天；\n",
      "上涨趋势从【2020-11-03】开始， 到【2020-11-11】结束; 持续【6】天；\n",
      "上涨趋势从【2020-11-16】开始， 到【2020-11-24】结束; 持续【6】天；\n",
      "上涨趋势从【2020-12-30】开始， 到【2021-01-14】结束; 持续【10】天；\n",
      "上涨趋势从【2021-08-30】开始， 到【2021-09-09】结束; 持续【8】天；\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def trend_split(df): \n",
    "    trendtype = 0\n",
    "    upcount = 0\n",
    "    continue_count = 5\n",
    "    for i in range(1, len(df)):\n",
    "        indexDate = df.index[i]\n",
    "        # 上涨\n",
    "        if df.High[i] >= df.High[i - 1]:\n",
    "            if trendtype != 1: # 第一次满足上涨趋势\n",
    "                trendtype = 1\n",
    "                upcount = 0\n",
    "                trend_up_start = indexDate\n",
    "            upcount=upcount+1\n",
    "            continue # 不能继续执行下面的语句，否则会将trendtype设置为0\n",
    "            \n",
    "        else:\n",
    "            if trendtype == 1: # 上涨结束\n",
    "                #记录结果\n",
    "                if upcount > continue_count:\n",
    "                    print('上涨趋势从【%s】开始， 到【%s】结束; 持续【%d】天；'%(trend_up_start, indexDate, upcount))\n",
    "                # 清空数据\n",
    "                trendtype =  0\n",
    "                upcount = 0 \n",
    "        # 下跌\n",
    "        if df.Low[i] <= df.Low[i - 1]:\n",
    "            if trendtype != 2: # 第一次满足下跌趋势\n",
    "                trendtype = 2\n",
    "                downcount = 0\n",
    "                trend_down_start = indexDate\n",
    "                \n",
    "            downcount=downcount+1          \n",
    "        else:\n",
    "            if trendtype == 2: # 下跌结束\n",
    "                #记录结果\n",
    "                if downcount > continue_count:\n",
    "                    print('下跌趋势从【%s】开始， 到【%s】结束; 持续【%d】天；'%(trend_down_start, indexDate, downcount))\n",
    "                # 清空数据\n",
    "                trendtype =  0\n",
    "                downcount = 0\n",
    "    print('end')\n",
    "    \n",
    "trend_split(df1)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "上涨趋势从【2015-03-06】开始， 到【2015-03-25】结束; 持续【13】天；\n",
      "上涨趋势从【2015-04-02】开始， 到【2015-04-21】结束; 持续【12】天；\n",
      "上涨趋势从【2015-05-19】开始， 到【2015-05-29】结束; 持续【8】天；\n",
      "上涨趋势从【2015-06-01】开始， 到【2015-06-11】结束; 持续【8】天；\n",
      "上涨趋势从【2015-07-17】开始， 到【2015-07-27】结束; 持续【6】天；\n",
      "上涨趋势从【2015-10-14】开始， 到【2015-10-22】结束; 持续【6】天；\n",
      "上涨趋势从【2015-12-14】开始， 到【2015-12-24】结束; 持续【8】天；\n",
      "上涨趋势从【2016-06-28】开始， 到【2016-07-08】结束; 持续【8】天；\n",
      "上涨趋势从【2016-08-03】开始， 到【2016-08-17】结束; 持续【10】天；\n",
      "上涨趋势从【2017-01-03】开始， 到【2017-01-11】结束; 持续【6】天；\n",
      "上涨趋势从【2017-07-19】开始， 到【2017-07-27】结束; 持续【6】天；\n",
      "上涨趋势从【2017-10-20】开始， 到【2017-10-30】结束; 持续【6】天；\n",
      "上涨趋势从【2017-12-29】开始， 到【2018-01-11】结束; 持续【8】天；\n",
      "上涨趋势从【2018-01-17】开始， 到【2018-01-30】结束; 持续【9】天；\n",
      "上涨趋势从【2019-01-11】开始， 到【2019-01-22】结束; 持续【7】天；\n",
      "上涨趋势从【2019-01-31】开始， 到【2019-02-15】结束; 持续【6】天；\n",
      "上涨趋势从【2019-03-28】开始， 到【2019-04-09】结束; 持续【7】天；\n",
      "上涨趋势从【2019-12-25】开始， 到【2020-01-03】结束; 持续【6】天；\n",
      "上涨趋势从【2020-04-29】开始， 到【2020-05-12】结束; 持续【6】天；\n",
      "上涨趋势从【2020-06-30】开始， 到【2020-07-10】结束; 持续【8】天；\n",
      "上涨趋势从【2020-07-28】开始， 到【2020-08-05】结束; 持续【6】天；\n",
      "上涨趋势从【2020-11-03】开始， 到【2020-11-11】结束; 持续【6】天；\n",
      "上涨趋势从【2020-11-16】开始， 到【2020-11-24】结束; 持续【6】天；\n",
      "上涨趋势从【2020-12-30】开始， 到【2021-01-14】结束; 持续【10】天；\n",
      "上涨趋势从【2021-08-30】开始， 到【2021-09-09】结束; 持续【8】天；\n"
     ]
    }
   ],
   "source": [
    "# trend_break(df1)\n",
    "def trend_up(df):   \n",
    "    trendtype = 0\n",
    "    upcount = 0\n",
    "    for i in range(1, len(df)):\n",
    "        indexDate = df.index[i]\n",
    "        # 上涨\n",
    "        if df.High[i] >= df.High[i - 1]:\n",
    "            if trendtype != 1: # 第一次满足上涨趋势\n",
    "                trendtype = 1\n",
    "                upcount = 0\n",
    "                trend_up_start = indexDate\n",
    "            upcount=upcount+1\n",
    "        else:\n",
    "            if trendtype == 1: # 上涨结束\n",
    "                #记录结果\n",
    "                if upcount > 5:\n",
    "                    print('上涨趋势从【%s】开始， 到【%s】结束; 持续【%d】天；'%(trend_up_start, indexDate, upcount))\n",
    "                # 清空数据\n",
    "                trendtype =  0\n",
    "                upcount = 0                \n",
    "            upcount=0\n",
    "            trendtype = 0\n",
    "\n",
    "trend_up(df1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "上涨趋势从【2015-03-06】开始， 到【2015-03-25】结束; 持续【13】天；\n",
      "上涨趋势从【2015-04-02】开始， 到【2015-04-21】结束; 持续【12】天；\n",
      "上涨趋势从【2015-05-19】开始， 到【2015-05-29】结束; 持续【8】天；\n",
      "上涨趋势从【2015-06-01】开始， 到【2015-06-11】结束; 持续【8】天；\n",
      "上涨趋势从【2015-07-17】开始， 到【2015-07-27】结束; 持续【6】天；\n",
      "上涨趋势从【2015-10-14】开始， 到【2015-10-22】结束; 持续【6】天；\n",
      "上涨趋势从【2015-12-14】开始， 到【2015-12-24】结束; 持续【8】天；\n",
      "上涨趋势从【2016-06-28】开始， 到【2016-07-08】结束; 持续【8】天；\n",
      "上涨趋势从【2016-08-03】开始， 到【2016-08-17】结束; 持续【10】天；\n",
      "上涨趋势从【2017-01-03】开始， 到【2017-01-11】结束; 持续【6】天；\n",
      "上涨趋势从【2017-07-19】开始， 到【2017-07-27】结束; 持续【6】天；\n",
      "上涨趋势从【2017-10-20】开始， 到【2017-10-30】结束; 持续【6】天；\n",
      "上涨趋势从【2017-12-29】开始， 到【2018-01-11】结束; 持续【8】天；\n",
      "上涨趋势从【2018-01-17】开始， 到【2018-01-30】结束; 持续【9】天；\n",
      "上涨趋势从【2019-01-11】开始， 到【2019-01-22】结束; 持续【7】天；\n",
      "上涨趋势从【2019-01-31】开始， 到【2019-02-15】结束; 持续【6】天；\n",
      "上涨趋势从【2019-03-28】开始， 到【2019-04-09】结束; 持续【7】天；\n",
      "上涨趋势从【2019-12-25】开始， 到【2020-01-03】结束; 持续【6】天；\n",
      "上涨趋势从【2020-04-29】开始， 到【2020-05-12】结束; 持续【6】天；\n",
      "上涨趋势从【2020-06-30】开始， 到【2020-07-10】结束; 持续【8】天；\n",
      "上涨趋势从【2020-07-28】开始， 到【2020-08-05】结束; 持续【6】天；\n",
      "上涨趋势从【2020-11-03】开始， 到【2020-11-11】结束; 持续【6】天；\n",
      "上涨趋势从【2020-11-16】开始， 到【2020-11-24】结束; 持续【6】天；\n",
      "上涨趋势从【2020-12-30】开始， 到【2021-01-14】结束; 持续【10】天；\n",
      "上涨趋势从【2021-08-30】开始， 到【2021-09-09】结束; 持续【8】天；\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "# items = zip(range(len(df1)), df1.iterrows())\n",
    "#df1.rename( columns={'times':'Date','openp':'Open', 'highp':'High','lowp':'Low',\"nowv\":'Close','curvol':\"Volume\"}, inplace=True)\n",
    "# trend_up(df1)\n",
    "#trend_down(df1)\n",
    "\n",
    "trend_split(df1)\n",
    "#df1.head()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "trend_up=[]\n",
    "trend_down=[]\n",
    "trend_updown=[]\n",
    "\n",
    "for idx, (date,row) in items:\n",
    "    print(idx)\n",
    "    print(date)\n",
    "    print(row)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下跌趋势从【2015-12-04】开始， 到【2015-12-15】结束; 持续【7】天；\n",
      "下跌趋势从【2016-01-07】开始， 到【2016-01-15】结束; 持续【6】天；\n",
      "下跌趋势从【2016-05-05】开始， 到【2016-05-13】结束; 持续【6】天；\n",
      "下跌趋势从【2017-05-02】开始， 到【2017-05-12】结束; 持续【8】天；\n",
      "下跌趋势从【2018-05-22】开始， 到【2018-05-31】结束; 持续【7】天；\n",
      "下跌趋势从【2018-06-26】开始， 到【2018-07-04】结束; 持续【6】天；\n",
      "下跌趋势从【2018-09-05】开始， 到【2018-09-13】结束; 持续【6】天；\n",
      "下跌趋势从【2018-12-14】开始， 到【2018-12-24】结束; 持续【6】天；\n",
      "下跌趋势从【2019-04-19】开始， 到【2019-04-30】结束; 持续【7】天；\n",
      "下跌趋势从【2019-07-03】开始， 到【2019-07-16】结束; 持续【9】天；\n",
      "下跌趋势从【2020-07-10】开始， 到【2020-07-20】结束; 持续【6】天；\n",
      "下跌趋势从【2020-09-01】开始， 到【2020-09-14】结束; 持续【9】天；\n",
      "下跌趋势从【2020-12-03】开始， 到【2020-12-14】结束; 持续【7】天；\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "def trend_down(df): \n",
    "    trendtype = 0\n",
    "    for i in range(1, len(df)):\n",
    "        indexDate = df.index[i]\n",
    "        # 下跌\n",
    "        if df.Low[i] <= df.Low[i - 1]:\n",
    "            if trendtype != 2: # 第一次满足下跌趋势\n",
    "                trendtype = 2\n",
    "                downcount = 0\n",
    "                trend_down_start = indexDate\n",
    "                \n",
    "            downcount=downcount+1            \n",
    "        else:\n",
    "            if trendtype == 2: # 下跌结束\n",
    "                #记录结果\n",
    "                if downcount > 5:\n",
    "                    print('下跌趋势从【%s】开始， 到【%s】结束; 持续【%d】天；'%(trend_down_start, indexDate, downcount))\n",
    "                # 清空数据\n",
    "                trendtype =  0\n",
    "                upcount = 0\n",
    "                \n",
    "            downcount=0\n",
    "            trendtype = 0\n",
    "        \n",
    "    print('end')\n",
    "    \n",
    "trend_down(df1)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>High</th>\n",
       "      <th>Open</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>preclose</th>\n",
       "      <th>Volume</th>\n",
       "      <th>curvalue</th>\n",
       "      <th>signType</th>\n",
       "      <th>dkWarnType</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>times</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-10-15</th>\n",
       "      <td>3578.77</td>\n",
       "      <td>3551.99</td>\n",
       "      <td>3542.69</td>\n",
       "      <td>3572.37</td>\n",
       "      <td>3558.28</td>\n",
       "      <td>3.202930e+10</td>\n",
       "      <td>4.259247e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-18</th>\n",
       "      <td>3571.05</td>\n",
       "      <td>3571.05</td>\n",
       "      <td>3539.48</td>\n",
       "      <td>3568.14</td>\n",
       "      <td>3572.37</td>\n",
       "      <td>3.409506e+10</td>\n",
       "      <td>4.557879e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-19</th>\n",
       "      <td>3596.79</td>\n",
       "      <td>3562.30</td>\n",
       "      <td>3560.62</td>\n",
       "      <td>3593.15</td>\n",
       "      <td>3568.14</td>\n",
       "      <td>3.341977e+10</td>\n",
       "      <td>4.401815e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-20</th>\n",
       "      <td>3596.05</td>\n",
       "      <td>3583.24</td>\n",
       "      <td>3574.30</td>\n",
       "      <td>3587.00</td>\n",
       "      <td>3593.15</td>\n",
       "      <td>3.442450e+10</td>\n",
       "      <td>4.504830e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-21</th>\n",
       "      <td>3610.96</td>\n",
       "      <td>3590.05</td>\n",
       "      <td>3576.35</td>\n",
       "      <td>3594.78</td>\n",
       "      <td>3587.00</td>\n",
       "      <td>3.523109e+10</td>\n",
       "      <td>4.535717e+11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               High     Open      Low    Close  preclose        Volume  \\\n",
       "times                                                                    \n",
       "2021-10-15  3578.77  3551.99  3542.69  3572.37   3558.28  3.202930e+10   \n",
       "2021-10-18  3571.05  3571.05  3539.48  3568.14   3572.37  3.409506e+10   \n",
       "2021-10-19  3596.79  3562.30  3560.62  3593.15   3568.14  3.341977e+10   \n",
       "2021-10-20  3596.05  3583.24  3574.30  3587.00   3593.15  3.442450e+10   \n",
       "2021-10-21  3610.96  3590.05  3576.35  3594.78   3587.00  3.523109e+10   \n",
       "\n",
       "                curvalue  signType  dkWarnType  \n",
       "times                                           \n",
       "2021-10-15  4.259247e+11         0           0  \n",
       "2021-10-18  4.557879e+11         0           0  \n",
       "2021-10-19  4.401815e+11         0           0  \n",
       "2021-10-20  4.504830e+11         0           0  \n",
       "2021-10-21  4.535717e+11         0           0  "
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'df' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-ae948e3dbdac>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# candle.candlePlot(df)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrename\u001b[0m\u001b[1;33m(\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'times'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m'Date'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'openp'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m'Open'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'highp'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m'High'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'lowp'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m'Low'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"nowv\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m'Close'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'curvol'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"Volume\"\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minplace\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mcandle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcandlePlot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'df' is not defined"
     ]
    }
   ],
   "source": [
    "# candle.candlePlot(df)\n",
    "\n",
    "candle.candlePlot(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already up-to-date: mplfinance in c:\\programdata\\anaconda3\\lib\\site-packages (0.12.7a17)\n",
      "Requirement already satisfied, skipping upgrade: pandas in c:\\programdata\\anaconda3\\lib\\site-packages (from mplfinance) (1.0.5)\n",
      "Requirement already satisfied, skipping upgrade: matplotlib in c:\\programdata\\anaconda3\\lib\\site-packages (from mplfinance) (3.2.2)\n",
      "Requirement already satisfied, skipping upgrade: python-dateutil>=2.6.1 in c:\\users\\ldq\\appdata\\roaming\\python\\python38\\site-packages (from pandas->mplfinance) (2.8.0)\n",
      "Requirement already satisfied, skipping upgrade: numpy>=1.13.3 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas->mplfinance) (1.20.3)\n",
      "Requirement already satisfied, skipping upgrade: pytz>=2017.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas->mplfinance) (2020.1)\n",
      "Requirement already satisfied, skipping upgrade: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib->mplfinance) (2.4.7)\n",
      "Requirement already satisfied, skipping upgrade: cycler>=0.10 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib->mplfinance) (0.10.0)\n",
      "Requirement already satisfied, skipping upgrade: kiwisolver>=1.0.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib->mplfinance) (1.2.0)\n",
      "Requirement already satisfied, skipping upgrade: six>=1.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from python-dateutil>=2.6.1->pandas->mplfinance) (1.15.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "# 1.0 连续10日以上才是趋势； 否则视为震荡\n",
    "def trend_up(df):\n",
    "    count = 0\n",
    "    for i in range(1, len(df)):\n",
    "        if df.High[i] >= df.High[i - 1]:\n",
    "            count = count + 1\n",
    "        else:\n",
    "            count = 0\n",
    "        if count > 10:\n",
    "            return True\n",
    "        \n",
    "    return False    \n",
    "\n",
    "def trend_down(df):\n",
    "    count = 0\n",
    "    for i in range(1, len(df)):\n",
    "        if df.High[i] >= df.High[i - 1]:\n",
    "            count = count + 1\n",
    "        else:\n",
    "            count = 0\n",
    "            \n",
    "        if count > 10:\n",
    "            print('yes')\n",
    "            return True\n",
    "        \n",
    "    return False\n",
    "\n",
    "def trend_updown(df): \n",
    "    count = 0\n",
    "    for i in range(1, len(df)):\n",
    "        if df.High[i] >= df.High[i - 1]:\n",
    "            pass\n",
    "        else:\n",
    "            print(df.index[i])\n",
    "        \n",
    "def trend_split(df): \n",
    "    trendtype = 0\n",
    "    \n",
    "    for i in range(1, len(df)):\n",
    "        indexDate = df.index[i]\n",
    "        # 上涨\n",
    "        if df.High[i] >= df.High[i - 1]:\n",
    "            if trendtype != 1: # 第一次满足上涨趋势\n",
    "                trendtype = 1\n",
    "                upcount = 0\n",
    "                trend_up_start = indexDate\n",
    "                \n",
    "            upcount=upcount+1            \n",
    "        else:\n",
    "            if trendtype ==1: # 上涨结束\n",
    "                #记录结果\n",
    "                if upcount > 2:\n",
    "                    print('上涨趋势从【%s】开始， 到【%s】结束; 持续【%d】天；'%(trend_up_start, indexDate, upcount))\n",
    "                # 清空数据\n",
    "                trendtype =  0\n",
    "                upcount = 0\n",
    "                break\n",
    "                \n",
    "            upcount=0\n",
    "            trendtype = 0\n",
    "            \n",
    "        # 下跌\n",
    "        if df.Low[i] <= df.Low[i - 1]:\n",
    "            if trendtype != 2: # 第一次满足下跌趋势\n",
    "                trendtype = 2\n",
    "                downcount = 0\n",
    "                trend_down_start = indexDate\n",
    "                \n",
    "            downcount=downcount+1            \n",
    "        else:\n",
    "            if trendtype == 2: # 下跌结束\n",
    "                #记录结果\n",
    "                if downcount > 2:\n",
    "                    print('下跌趋势从【%s】开始， 到【%s】结束; 持续【%d】天；'%(trend_up_start, indexDate, upcount))\n",
    "                # 清空数据\n",
    "                trendtype =  0\n",
    "                upcount = 0\n",
    "                break\n",
    "                \n",
    "            downcount=0\n",
    "            trendtype = 0\n",
    "        \n",
    "    print('end')\n",
    "    \n",
    "    \n",
    "\n",
    "def trend_break(df): \n",
    "    # maxHigh = np.iinfo(np.float32).min\n",
    "    # minLow = np.iinfo(np.float32).max\n",
    "    maxHigh = df.High[0] \n",
    "    minLow = df.Low[0]\n",
    "    listup=list()\n",
    "    listdown=list()\n",
    "    for i in range(0, len(df)):\n",
    "        if df.High[i] > maxHigh:\n",
    "            listup.append('在【{%s}】向上突破[%f]'%(df.index[i], maxHigh))\n",
    "            maxHigh = df.High[i]\n",
    "        \n",
    "        if df.Low[i] > minLow:\n",
    "            listdown.append('在【{%s}】向下突破[%f]'%(df.index[i], minLow))\n",
    "            minLow =   df.Low[i]  \n",
    "    print(listup)\n",
    "    print(listdown)\n",
    "    print('end')    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
